var twoSum = function(nums, target) {
  const len = nums.length;
  const map = new Map();
  let ans = [];

  for (let i = 0; i < len; i++) {
    if (map.has(nums[i])) {
      ans.push(map.get(nums[i]), i);
    }
    map.set(target - nums[i], i);
  }
  return ans;
};

const nums = [2,7,11,15], target = 9;
console.log(twoSum(nums, target))